package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("PunishController")
public class PunishController extends CmiEmaController {

	@RequestMapping("/listPunishs")
	public ModelAndView listPunishs(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = punishPaginationService.updatePagination(request, pagination);
		Set<Punish> punishs = punishPaginationService.getPaginationPunishs(pagination);

		mav.addObject("punishs", punishs);
		mav.addObject("entityName", "punish");
		mav.addObject("pagination", pagination);
		mav.setViewName("punish/listPunishs.jsp");
		return mav;
	}

	public String listPunishs() {
		return "redirect:/listPunishs";
	}

	@RequestMapping("/viewPunish")
	public ModelAndView viewPunish(@RequestParam Integer punishId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("punish", punishDAO.findPunishByPrimaryKey(punishId));
		mav.setViewName("punish/viewPunish.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeletePunishs")
	public ModelAndView confirmDeletePunishs(
			@RequestParam(value = "deletePunishsIds", required = true) Set<Integer> deletePunishsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Punish> deletePunishs = punishDAO.findPunishsByIds(deletePunishsIds);
		Set<Punish> referencedPunishs = punishService.loadReferencePunishs(deletePunishs);
		boolean hasReferenced;
		if (referencedPunishs.size() == 0) {
			mav.addObject("deleteEntitys", deletePunishs);
			hasReferenced = false;
		} else {
			deletePunishs.removeAll(referencedPunishs);
			mav.addObject("referencedEntitys", referencedPunishs);
			mav.addObject("deleteEntitys", deletePunishs);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Punish");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listPunishs");
		return mav;
	}

	@RequestMapping("/deletePunish")
	public String deletePunish(@RequestParam Integer punishId) {
		Punish punish = punishDAO.findPunishByPrimaryKey(punishId);
		punishService.deletePunish(punish);
		return "forward:/listPunishs";
	}

	@RequestMapping("/confirmDeletePunish")
	public ModelAndView confirmDeletePunish(@RequestParam Integer punishId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("punish", punishDAO.findPunishByPrimaryKey(punishId));

		mav.setViewName("punish/deletePunish.jsp");

		return mav;
	}

	@RequestMapping("/deletePunishs")
	public String deletePunishs(
			@RequestParam(value = "deletePunishsId", required = true) Set<Integer> deletePunishsId) {
		punishService.deletePunishs(deletePunishsId);
		return "forward:/listPunishs";
	}

	@RequestMapping("/editPunish")
	public ModelAndView editPunish(@RequestParam Integer punishId) {

		ModelAndView mav = new ModelAndView();

		Punish punish = punishDAO.findPunishByPrimaryKey(punishId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("punish", punish);

		// 把所有的引用punish对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(punishService.loadPunishObjectReferencedSet(punish));

		// 将编辑页面punish添加到mav中
		mav.setViewName("punish/editPunish.jsp");
		return mav;
	}

	@RequestMapping("/newPunish")
	public ModelAndView newPunish() {
		ModelAndView mav = new ModelAndView();
		Punish punish = new Punish();

		mav.addObject("punish", punish);

		// 把所有的引用punish对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(punishService.loadPunishObjectReferencedSet(punish));

		mav.addObject("newFlag", true);
		mav.setViewName("punish/editPunish.jsp");

		return mav;
	}

	@RequestMapping("/savePunish")
	public ModelAndView savePunish(@ModelAttribute Punish punish) {

		ModelAndView mav = new ModelAndView();

		Punish existingPunish = CmiHibernateProxyUtil
				.deproxy(punishDAO.findPunishByPhysicalPrimaryKey(punish.getName()));

		// 新建记录
		if (punish.getId() == null) {
			if (punish.equals(existingPunish)) {

				mav.addAllObjects(punishService.loadPunishClassReferencedSet(punish));
				// mav = editContinue(mav, punish);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("punish/editPunish.jsp");
			} else {
				punishService.savePunish(punish);
				mav.setViewName("forward:/listPunishs");
			}
		}
		// 编辑记录
		else {
			Punish originPunish = CmiHibernateProxyUtil.deproxy(punishDAO.findPunishByPrimaryKey(punish.getId()));
			if (!punish.equals(originPunish) && punish.equals(existingPunish)) {
				mav.addAllObjects(punishService.loadPunishClassReferencedSet(punish));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("punish/editPunish.jsp");
			} else {
				punishService.savePunish(punish);
				mav.setViewName("forward:/listPunishs");
			}
		}
		return mav;
	}

	@RequestMapping("/punishController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
